package cn.chenlh.fx.test.demo;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class MCVE extends Application {
    public void start(Stage stage) {
        HBox root = new HBox();
        ObservableList<String> items = FXCollections.observableArrayList(
                "One", "Two", "Three", "Four", "Five", "Six",
                "Seven", "Eight", "Nine", "Ten",
                "天堂", "你的", "类似的", "米尔萨普"
        );
        FilteredList<String> filteredItems = new FilteredList<>(items, p -> true);

        ComboBox<String> cb = new ComboBox<>();
        cb.setEditable(true);


        cb.getEditor().textProperty().addListener((obs, oldValue, newValue) -> {
            final TextField editor = cb.getEditor();
            final String selected = cb.getSelectionModel().getSelectedItem();
            Platform.runLater(() -> {
                if (selected == null || !selected.equals(editor.getText())) {
                    filteredItems.setPredicate(item ->
                            item.toUpperCase().startsWith(newValue.toUpperCase()));
                    cb.show();
                }
            });
        });
        cb.setItems(filteredItems);

        root.getChildren().add(cb);

        Scene scene = new Scene(root, 400, 50);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}